package com.studerb;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;

import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.Application.ExitListener;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.studerb.util.Util;
import com.studerb.view.GnusbinAboutBox;

class ConfirmExitListener implements ExitListener {
	@Override public boolean canExit(EventObject e) {
		Object source = e != null ? e.getSource() : null;
		Component owner = source instanceof Component ? (Component) source : null;
		int option = JOptionPane.showConfirmDialog(owner, "Really Exit?");
		return option == JOptionPane.YES_OPTION;
	}

	@Override public void willExit(EventObject e) {}
}

public class Gnusbin extends SingleFrameApplication {
	private static final Logger logger = Logger.getLogger(Gnusbin.class);
	protected static ClassPathXmlApplicationContext springAppContext;

	private JComponent mainPanel;
	private JMenuBar mainMenuBar;
	private JPanel statusBar;
	GnusbinAboutBox aboutBox;

	public Gnusbin() {
		super();
	}

	public static void main(String[] args) {
		try {
			logger.debug("Starting Spring Application Context");
			//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()
			// );
			com.jidesoft.utils.Lm.verifyLicense("Bill Studer", "Gnusbin", "GUPxWo:0hpUtR3u94.K90:R7sPr.VCv2");
			UIManager.setLookAndFeel(new com.jgoodies.looks.plastic.Plastic3DLookAndFeel());
			springAppContext = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
			// set log4j to check the properties file every ten seconds
			PropertyConfigurator.configureAndWatch("log4j.properties", 10000);
			springAppContext.registerShutdownHook();
			Application.launch(Gnusbin.class, null);
		}
		catch (Exception e) {
			e.printStackTrace();
			System.err.println(ExceptionUtils.getRootCauseMessage(e));
			Application.getInstance(Gnusbin.class).end();
		}
	}

	private void initMainComponent() {
		mainPanel = (JComponent) springAppContext.getBean("mainPanel");
		getMainFrame().add(mainPanel, BorderLayout.CENTER);
		Util.setSwingApplication(this);
	}

	private void initMenu() {
		mainMenuBar = new JMenuBar();
		JMenu fileMenu = new JMenu();
		fileMenu.setName("fileMenu");
		// Exit
		JMenuItem exitMenuItem = new JMenuItem();
		exitMenuItem.setName("exitMenuItem");
		exitMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Application.getInstance().exit();
			}
		});
		fileMenu.add(exitMenuItem);

		// Edit
		JMenu editMenu = new JMenu();
		editMenu.setName("editMenu");

		JMenu helpMenu = new JMenu();
		helpMenu.setName("helpMenu");
		JMenuItem aboutMenuItem = new JMenuItem();
		aboutMenuItem.setName("aboutMenuItem");
		aboutMenuItem.setAction(getContext().getActionMap().get("showAboutBox"));
		helpMenu.add(aboutMenuItem);

		mainMenuBar.add(fileMenu);
		mainMenuBar.add(editMenu);
		mainMenuBar.add(helpMenu);
		getMainFrame().setJMenuBar(mainMenuBar);
	}

	private void initStatusBar() {
		statusBar = (JPanel) springAppContext.getBean("statusBar");
		getMainFrame().add(statusBar, BorderLayout.SOUTH);
	}

	@Action public void showAboutBox() {
		if (aboutBox == null) {
			aboutBox = new GnusbinAboutBox(getMainFrame());
			aboutBox.setLocationRelativeTo(getMainFrame());
		}
		show(aboutBox);
	}

	@Override protected void shutdown() {
		logger.debug("Shutting Down");
		super.shutdown();
		hide(getMainView());
		logger.debug("Closing Spring AppContext");
		Gnusbin.springAppContext.close();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jdesktop.application.Application#startup() Start up the Gui
	 */
	@Override protected void startup() {
		addExitListener(new ConfirmExitListener());
		initMainComponent();
		initMenu();
		initStatusBar();
		show(mainPanel);
	}
}
